package com.source.lightfowl.basedata.service.impl;

import com.source.lightfowl.DTO.PoultryParentDownloadDTO;
import com.source.lightfowl.DTO.PoultryParentUploadDTO;
import com.source.lightfowl.basedata.dao.PoultryParentDao;
import com.source.lightfowl.basedata.entity.Poultry_Parent;
import com.source.lightfowl.basedata.service.PoultryParentService;
import com.source.lightfowl.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: Faink
 * @CreateTime: 2020/10/27 19:52
 */
@Service
public class PoultryParentServiceImpl implements PoultryParentService {
    @Autowired
    private PoultryParentDao poultryParentDao;

    @Override
    public int save(Poultry_Parent parent) {
        String nameById = poultryParentDao.findNameById(parent.getId());
        if (nameById!=null && nameById.equals(parent.getPoultryParentName())) {
            return 0;
        } else {
            parent.setCreateDate(new Date());
            poultryParentDao.save(parent);
        }
        return 1;
    }

    //逻辑删除
    @Override
    public void deleteById(String id) {
        poultryParentDao.deleteById(id);
    }

    @Override
    public int updateById(Poultry_Parent parent) {
        String nameById = poultryParentDao.findNameById(parent.getId());
        if (nameById!=null && nameById.equals(parent.getPoultryParentName())) {
            return 0;
        }
        parent.setCreateDate(new Date());
        poultryParentDao.updateName(parent.getPoultryParentName(), parent.getId());
        return 1;
    }

    @Override
    public List<Poultry_Parent> findAll() {
        List<Poultry_Parent> all = poultryParentDao.findAll();
        return all;
    }

    @Override
    public Poultry_Parent findOne(String id) {
        return poultryParentDao.findOne(id);
    }

    @Override
    public void save(List<Object> list, HttpServletRequest request) {
        List<Poultry_Parent> poultryParentList = new ArrayList<>();
        for (Object object:
             list) {
            Poultry_Parent poultryParent = new Poultry_Parent();
            PoultryParentUploadDTO uploadDTO = (PoultryParentUploadDTO) object;
            poultryParent.setId(uploadDTO.getId());
            poultryParent.setPoultryParentName(uploadDTO.getPoultryParentName());
            poultryParent.setIsDelete(0);
            poultryParent.setCreateDate(new Date());
            poultryParentList.add(poultryParent);
        }
        poultryParentDao.saveAll(poultryParentList);
    }

    @Override
    public List<Object> findAllDTO() {
        List<Poultry_Parent> parentDaoAll = poultryParentDao.findAll();
        List<Object> list = new ArrayList<>();
        for (Poultry_Parent poultryParent :
                parentDaoAll) {
            PoultryParentDownloadDTO downloadDTO = new PoultryParentDownloadDTO();
            downloadDTO.setId(poultryParent.getId());
            downloadDTO.setPoultryParentName(poultryParent.getPoultryParentName());
            downloadDTO.setCreateDate(new DateUtils().stringToYMD(poultryParent.getCreateDate()));
            list.add(downloadDTO);
        }
        return list;
    }
}
